﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace NetworkManager
{
	public class Mgr
	{
		public static int TI(string value)
		{
			try { return Convert.ToInt32(value); }
			catch { return 0; }
		}
		public static DateTime StampToTime(int timestamp)
		{
			DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, 0);
			date = date.AddSeconds(timestamp);
			return date;
		}
		public static int TimeToStamp(DateTime date)
		{
			TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0));
			int timestamp = (int)t.TotalSeconds;
			return timestamp;
		}
		public static string ResponseToString(HttpWebResponse re)
		{
			StringBuilder sb = new StringBuilder();
			byte[] buf = new byte[8192];
			HttpWebResponse response = re;
			Stream resStream = response.GetResponseStream();
			string tempString = null;
			int count = 0;
			do
			{
				// fill the buffer with data
				count = resStream.Read(buf, 0, buf.Length);

				// make sure we read some data
				if (count != 0)
				{
					// translate from bytes to ASCII text
					tempString = Encoding.ASCII.GetString(buf, 0, count);

					// continue building the string
					sb.Append(tempString);
				}
			}
			while (count > 0); // any more data to read?
			return sb.ToString();
		}
		public static string[] GetMergedArray(string[] originalArray, string[] newArray)
		{
			int startIndexForNewArray = originalArray.Length;
			Array.Resize<string>(ref originalArray, originalArray.Length + newArray.Length);
			newArray.CopyTo(originalArray, startIndexForNewArray);
			return originalArray;
		}


	}
}
